<%= will_filter_scripts_tag %>

<%= form_tag("", :id => "tr8n_form", :method => :get) do %>
  <%=hidden_field_tag(:type, "translation_key") %>
  <%=hidden_field_tag(:id, @source.id) %>
  <%=hidden_field_tag(:mode, params[:mode]) %>
  <div style="border:1px solid #ccc;padding:10px;background:#eee;margin-top:10px;margin-bottom:10px;">
    <%= text_field_tag(:q, params[:q], :style => "width:88%", :placeholder => "Search for specific keys within this source") %>
    <%= link_to_function("<span>#{tra("Search")}</span>".html_safe, "Tr8n.Effects.submit('tr8n_form');", :class => "tr8n_grey_button tr8n_pcb") %>
    <%= link_to_function("<span>#{tra("Clear")}</span>".html_safe, "Tr8n.element('q').value = '';Tr8n.Effects.submit('tr8n_form');", :class => "tr8n_grey_button tr8n_pcb") %>
  </div>

  <%= will_filter_table_tag(@results, :columns => [
    [:icn, image_tag("tr8n/icons/key.png"), :title => "", :style => "width:12px;"],
    [:checkbox, :ids],
    :id,
    [:label, lambda{|key|
      html = [link_to(key.label, :controller => "/tr8n/admin/translation_key", :action => :view, :id => key.id) ]
      html << '<div style="color:grey; font-size:10px; padding-top:3px;">'
      html << key.description.to_s  
      html << '</div>' 
      html.join("").html_safe 
    }],
    [:translation_count, lambda{|key| link_to(key.translation_count || 0, :controller => "/tr8n/admin/translation_key", :action => :view, :id => key.id, :mode => :translations)}, 'padding:5px;text-align:right;'],
    [:created_at, lambda{|key| key.created_at}, 'padding:5px;text-align:right;'],
    [:actions, lambda{|key|
      tr8n_actions_tag([
        link_to_function("Update", "Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_update?id=#{key.id}', {height:450})"),
        link_to_function("Delete", "deleteObjects('#{key.id}')")
      ])
    }, 'padding:5px;text-align:right;white-space:nowrap;']
  ]) %>
<% end %>

<%= will_filter_actions_bar_tag(@results, [
  ['Remove From Source', 'removeSelectedKeysFromSource()'],
  ['Merge Selected Keys', 'mergeSelectedKeys()'],
  ['Delete Selected Keys', 'deleteObjects()'],
  ['Add To Another Source', 'addSelectedKeysToSource()'],
]) %>

<script>
  function getSelectedKeyIds() {
    var i=0;
    var checkbox = Tr8n.element('wf_check_' + i);
    var keys = [];
    while (checkbox) {
      if (checkbox.checked) {
        keys.push(checkbox.value);
      }
      checkbox = Tr8n.element('wf_check_' + (++i));
    }
    return keys; 
  }

  function removeSelectedKeysFromSource() {
    if (!confirm('Make sure the keys are removed from the actual source code. Otherwise they will reappear here again. \nAre you sure you want to remove selected keys from this source?'))
       return;
    Tr8n.element('tr8n_form').action = "/tr8n/admin/sources/remove_keys_from_source";
    Tr8n.element('tr8n_form').submit();
  }

  function mergeSelectedKeys() {
    var keys = getSelectedKeyIds();
    if (keys.length < 2) {
      alert('At least two keys must be selected.');
      return;
    }
    Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_merge?keys=' + keys, {height:350});
  }

  function addSelectedKeysToSource() {
    var keys = getSelectedKeyIds();
    if (keys.length == 0) {
      alert('At least one key must be selected.');
      return;
    }
    Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_add_to_source?keys=' + keys, {height:350});
  }

</script>